package com.joymart.compute.repository;

import com.joymart.common.data.jdbc.BaseRepository;
import com.joymart.compute.model.MonthlyGroupPv;
import com.joymart.user.model.BusinessRole;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface MonthlyGroupPvRepository extends BaseRepository<MonthlyGroupPv, String> {


     @Modifying
     @Query(value = """
        delete from monthly_group_pv where month between :start and :end and scale = :scale
""" )
     void deleteAllBetween(BusinessRole scale, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

     @Query("""
                     select * from monthly_group_pv where user_id =:userId
                                                        and scale = :scale
                      and month between :start and :end  
             """)

     List<MonthlyGroupPv> findMonthlyGroupPv(String userId, BusinessRole scale, LocalDateTime start, LocalDateTime end);

     @Query("""
                     select count(*) from monthly_group_pv where user_id =:userId
                                                        and scale = :scale
                      and month between :start and :end  
                      and amount > :spec
             """)
     long countMonthlyGroupPvGreaterThan(String userId, BusinessRole scale, LocalDateTime start, LocalDateTime end, BigDecimal spec);
}
